package net.onmotion.network;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.microedition.io.HttpConnection;

public class MultipartHttpData extends NetworkData {

    public MultipartHttpData() {
        baos = new ByteArrayOutputStream();
        dos = new DataOutputStream(baos);
        first = true;
        boundary = "-------eded5b2abbfc469957835279e5b1db62";
        contentType = "multipart/form-data; boundary=" + boundary;
        method = HttpConnection.POST;
    }

    public void addPart(String key, String data) {
        try {
            dos.write(("--" + boundary + "\r\n").getBytes());
            dos.write(("Content-Disposition: form-data; name=\"" + key
                    + "\"\r\n\r\n").getBytes());
            dos.write((data + "\r\n").getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void addPart(String name, byte[] data, String type, String filename) {
        try {
            dos.write(("--" + boundary + "\r\n").getBytes());
            dos.write(("Content-Disposition: form-data; name=\"" + name
                    + "\"").getBytes());
            if (filename != null)
                dos.write(("; filename=\"" + filename + "\"").getBytes());
            dos.write(("\r\n").getBytes());
            if (type != null)
                dos.write(("Content-Type: " + type + "\r\n").getBytes());
            dos.write(("\r\n").getBytes());
            dos.write(data);
            dos.write(("\r\n").getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public void addPart(){
        try {
            dos.write(("--" + boundary + "\r\n").getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }    
            
}
